package com.wilmer.video.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.videoOrder.domain.VideoOrder;
import com.wilmer.videoOrder.service.IVideoOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.video.domain.Video;
import com.wilmer.video.domain.dto.VideoDto;
import com.wilmer.video.service.IVideoService;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.common.core.page.TableDataInfo;

/**
 * 视频Controller
 * 
 * @author qgf
 * @date 2024-04-01
 */
@RestController
@RequestMapping("/video/video")
public class VideoController extends BaseController
{
    @Autowired
    private IVideoService videoService;

    @Autowired
    private IVideoOrderService videoOrderService;
    /**
     * 查询视频列表（小程序端）
     */
    @ApiOperation("获取视频列表接口")
    @GetMapping("/getList")
    public AjaxResult videoList(VideoDto dto)
    {
        // 获取用户id
        Long userId = SecurityUtils.getUserId();
        List<VideoOrder> videoOrderDto = videoOrderService.selectVideoOrderByUserId(userId);
        List<VideoDto> list = videoService.selectList(dto);

        for (VideoDto element : list) {
            if(Objects.equals(element.getType(), "1")){
                element.setPay(1);
            } else {
                for (VideoOrder orderDto : videoOrderDto) {
                    if(orderDto.getVideoId().equals(element.getId()) && orderDto.getPay() == 1){
                        element.setPay(1);
                    }else{
                        element.setPay(0);
                    }
                }
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取视频详细信息（小程序端）
     */
    @ApiOperation("获取视频详情接口")
    @GetMapping("detail/{id}")
    public AjaxResult videoInfo(@PathVariable("id") Long id) {

        VideoDto videoDto = videoService.selectById(id);
        if (videoDto == null) {
            return AjaxResult.error("视频不存在");
        }
        return AjaxResult.success(videoDto);
    }
    /**
     * 查询视频列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(Video video)
    {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:export')")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video)
    {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "视频数据");
    }

    /**
     * 获取视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoService.selectVideoById(id));
    }

    /**
     * 新增视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:add')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Video video)
    {
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:edit')")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Video video)
    {
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 删除视频
     */
    @PreAuthorize("@ss.hasPermi('video:video:remove')")
    @Log(title = "视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoService.deleteVideoByIds(ids));
    }
}
